package com.zhisheng.day16_echarts.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhisheng.day16_echarts.dto.StudentScoreDto;
import com.zhisheng.day16_echarts.entity.StudentScore;
import com.zhisheng.day16_echarts.dao.StudentScoreDao;
import com.zhisheng.day16_echarts.service.StudentScoreService;
import com.zhisheng.day16_echarts.utils.ExcelToStudentScoreUtil;
import com.zhisheng.day16_echarts.vo.MyAvg;
import com.zhisheng.day16_echarts.vo.MyEchartsVo;
import jakarta.annotation.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (StudentScore)表服务实现类
 *
 * @author 全哥哥
 * @since 2025-04-30 14:56:21
 */
@Service("studentScoreService")
public class StudentScoreServiceImpl implements StudentScoreService {
    @Resource
    private StudentScoreDao studentScoreDao;

    private SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");

    private Map<String,List<StudentScore>> catchMap = new HashMap();

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public StudentScore queryById(Integer id) {
        return this.studentScoreDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param studentScore 实例对象
     * @return 实例对象
     */
    @Override
    public StudentScore insert(StudentScore studentScore) {
        this.studentScoreDao.insert(studentScore);
        return studentScore;
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.studentScoreDao.deleteById(id) > 0;
    }

    @Override
    public int insertByExcel(InputStream inputStream) {
        List<StudentScore> studentScores = ExcelToStudentScoreUtil.convertExcelToStudentScore(inputStream);
        int i = studentScoreDao.insertBatch(studentScores);
        return i;
    }

    @Override
    public Map<String, List> findAvgByGroupName() {

        List<MyAvg> avgByGroupName = studentScoreDao.findAvgByGroupName();
        List<String> names = new ArrayList<>();
        List<Double> scores = new ArrayList<>();
        for (MyAvg myAvg : avgByGroupName) {
            names.add(myAvg.getName());
            scores.add(myAvg.getPj());
        }
        Map<String, List> map = new HashMap<>();
        map.put("names",names);
        map.put("scores",scores);
        return map;
    }

    @Override
    public Map listByPage(StudentScoreDto dto) {
        Map map = new HashMap();
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<StudentScore> list = studentScoreDao.listByPage(dto);
        Page<StudentScore> page = (Page<StudentScore>) list;
        map.put("total",page.getTotal());
        map.put("data",page.getResult());
        return map;
    }

    @Override
    public Collection<MyEchartsVo> listByPageAndEcharts(StudentScoreDto dto) {
        // TODO 可以考虑缓存
        List<StudentScore> list = studentScoreDao.listByPageAndEcharts(dto);
        List<MyEchartsVo> res = new ArrayList<>();
        Map<String,MyEchartsVo> map = new HashMap<>();
        // 将相同学生姓名的数据合并
        for (StudentScore studentScore : list) {
            String name = studentScore.getName();
            MyEchartsVo echartsVo = map.getOrDefault(name, new MyEchartsVo(name,studentScore.getId()));
            Map temp = new HashMap();
            temp.put("score",studentScore.getScore());
            temp.put("time",sdf.format(studentScore.getSubmitTime()));
            echartsVo.getScores().add(temp);
            map.put(name,echartsVo);
        }
        Collection<MyEchartsVo> values = map.values();
        List<MyEchartsVo> voList = new ArrayList<>(values);
        Collections.sort(voList,(v1,v2)->Double.compare(v1.getAverageScore(), v2.getAverageScore()));
        return voList;
    }
}
